const app = getApp()
const util = require("../../utils/utils.js")
// pages/secondhand-collections/collections.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      //   {
      //   itemid: 'itemid',
      //   imgUrls: [
      //     "/assets/product_img/pro1.jpg", 
      //     "/assets/product_img/pro2.jpg", 
      //     "/assets/product_img/pro3.jpg",
      //     "/assets/product_img/pro3.jpg"
      //   ],
      //   title: 'title',
      //   price: '200.00',
      //   desc: 'ddddesc',
      //   publishTime: "2021-07-11",
      //   userinfo: {
      //     username: 'username',
      //     avatarUrl: "/assets/avatars/avatar1.jpg" 
      //   }
      // },{
      //   itemid: 'itemid',
      //   imgUrls: [
      //     "/assets/product_img/pro1.jpg", 
      //     "/assets/product_img/pro2.jpg", 
      //     "/assets/product_img/pro3.jpg",
      //     "/assets/product_img/pro3.jpg"
      //   ],
      //   title: 'title',
      //   price: '200.00',
      //   desc: 'ddddesc',
      //   publishTime: "2021-07-11",
      //   userinfo: {
      //     username: 'username',
      //     avatarUrl: "/assets/avatars/avatar1.jpg" 
      //   }
      // }
    ],
    userPhotos: [],
    userNicknames: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "我的收藏"
    });
    var that = this
    wx.request({
      url: 'http://localhost:8888/collecting/getCollectingByUser',
      method: 'GET',
      data: {
        username: app.globalData.loginAccount
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          items: res.data.data
        })
        for (var i = 0; i < that.data.items.length; i++) {
          var iPhoto = "items[" + i + "].photo"
          that.setData({
            [iPhoto]: util.strToArr(that.data.items[i].photo),
          })
        }
        //console.log(res.data.data);
        var length = res.data.data.length
        for(var i=0;i<length;i++) {
          that.getOwnerInfo(res.data.data[i].username, i)
        }
      }
    })
  },
  getOwnerInfo(userId, index) {
    var that = this
    wx.request({
      url: 'http://localhost:8888/account/getAccount',
      method: 'GET',
      data: {
        id: userId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var iPhoto = "userPhotos["+index+"]"
        var iNickname = "userNicknames["+index+"]"
        that.setData({
          [iPhoto]: res.data.data.headimg,
          [iNickname]: res.data.data.username
        })
        //console.log(that.data);
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onitemTapped(e) {
    var id = e.currentTarget.dataset.id
    app.globalData.dealingId = id
    wx.navigateTo({
      url: '../secondhand-detail/detail',
    })
  },
  preview(event) {
    wx.previewImage({
      current: this.data.items[event.currentTarget.dataset.iindex].photo[event.currentTarget.dataset.index],
      urls: this.data.items[event.currentTarget.dataset.iindex].photo,
    })
  },
  dontCollect(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    var thisItems = this.data.items
    var thisPhotos = this.data.userPhotos
    var thisNicknames = this.data.userNicknames
    wx.request({
      url: 'http://localhost:8888/collecting/deleteCollecting',
      method: 'POST',
      data: {
        username: app.globalData.loginAccount,
        dealing: this.data.items[index].id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        thisItems.splice(index, 1)
        thisPhotos.splice(index, 1)
        thisNicknames.splice(index, 1)
        that.setData({
          items: thisItems,
          userPhotos: thisPhotos,
          userNicknames: thisNicknames
        })
      }
    })
  }
})